import http from '../http'
import qs from 'qs'
import { request, requestOnline, ResponseCode, moment } from '../baseUrl'
/**
 *  @func 广告管理API
 *  @param request 请求地址 例如：http://197.82.15.15:8088/request/...
 *  @param '/testIp'代表vue-cil中config，index.js中配置的代理
 */

/**
 * @func 计划列表
 * @param { 加载列表 查询列表 }
 */
const advertPlanList = `/advertPlan/list`
export async function getAdvertPlanList({ pageNo = 1, pageSize = 100, planName, types = 0, status = [] }) {
  const _status = qs.stringify({ status: status }, { arrayFormat: 'repeat' })
  const qString = `${requestOnline}${advertPlanList}?${qs.stringify({ pageNo, pageSize, planName, types })}&${_status}`
  const response = await http.get(qString)
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 置顶
 * @param { id }
 */
const advertPlanTop = `/advertPlan/top`
export async function postAdvertPlanTop(id) {
  const response = await http.post(`${requestOnline}${advertPlanTop}`, qs.parse(id))
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 取消置顶
 * @param { id }
 */
const advertPlanUnTop = `/advertPlan/unTop`
export async function postAdvertPlanUnTop(id) {
  const response = await http.post(`${requestOnline}${advertPlanUnTop}`, qs.parse(id))
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 开启投放
 * @param {列表对象: record }
 */
const advertPlanStart = `/advertPlan/start`
export async function postAdvertPlanStart(record) {
  const response = await http.post(`${requestOnline}${advertPlanStart}`, qs.parse(record))
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 停止投放
 * @param {列表对象: record }
 */
const advertPlanStop = `/advertPlan/stop`
export async function postAdvertPlanStop(record) {
  const response = await http.post(`${requestOnline}${advertPlanStop}`, qs.parse(record))
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 查询素材列表
 * @param {列表对象: record }
 */
const materialList = `/material/list`
export async function getMaterialList({ pageNo = 1, pageSize = 100, advertType, name }) {
  const response = await http.get(`${requestOnline}${materialList}?${qs.stringify({ pageNo, pageSize, advertType, name })}`)
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 数据列表
 * @param {列表对象: record }
 */
const advertPlanData = `/advertPlan/data`
export async function getAdvertPlanData({
  pageNo = 1,
  pageSize = 100,
  advertType = '',
  id,
  name,
  startTime = moment(moment().format('YYYY-MM-DD')).valueOf(),
  endTime = moment(moment().format('YYYY-MM-DD'))
    .add(1, 'days')
    .valueOf(),
}) {
  const response = await http.get(
    `${requestOnline}${advertPlanData}?${qs.stringify({ pageNo, pageSize, advertType, id, name, startTime, endTime })}`,
  )
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 列表嵌套表格数据
 * @param {列表对象: record}
 */
const advertPlanDataDay = `/advertPlan/data/day`
export async function getAdvertPlanDataDay({
  id,
  startTime = moment(moment().format('YYYY-MM-DD')).valueOf(),
  endTime = moment(moment().format('YYYY-MM-DD'))
    .add(1, 'days')
    .valueOf(),
}) {
  const response = await http.get(`${requestOnline}${advertPlanDataDay}?${qs.stringify({ id, startTime, endTime })}`)
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 新建或编辑计划
 * @param {列表对象: record }
 */
const advertPlanUpdate = `/advertPlan/update`
export async function postAdvertPlanUpdate(record) {
  try {
    const response = await http.post(`${requestOnline}${advertPlanUpdate}`, qs.parse(record))
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}
/**
 * @func 计划编辑回显信息
 * @param {列表对象: record}
 */
const advertPlanGet = `/advertPlan/get`
export async function getAdvertPlanGet({ id }) {
  try {
    const response = await http.get(`${requestOnline}${advertPlanGet}?${qs.stringify({ id })}`)
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}

/**
 * @func 计划编辑下架信息
 * @param {列表对象: record}
 */
const advertPlanUnShow = `/advertPlan/unShow`
export async function postAdvertPlanUnShow(record) {
  try {
    const response = await http.post(`${requestOnline}${advertPlanUnShow}`, qs.parse(record))
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}

/**
 * @func 新建或编辑素材
 * @param {列表对象: record }
 */
const materialUpdate = `/material/update`
export async function postMaterialUpdate(record) {
  try {
    const response = await http.post(`${requestOnline}${materialUpdate}`, qs.parse(record))
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}

/**
 * @func 数据详情
 * @param {列表对象: record}
 */
const advertPlanDataSlot = `/advertPlan/data/slot`
export async function getAdvertPlanDataSlot({
  pageNo = 1,
  pageSize = 100,
  id,
  startTime = moment(moment().format('YYYY-MM-DD')).valueOf(),
  endTime = moment(moment().format('YYYY-MM-DD'))
    .add(1, 'days')
    .valueOf(),
}) {
  try {
    const response = await http.get(`${requestOnline}${advertPlanDataSlot}?${qs.stringify({ pageNo, pageSize, id, startTime, endTime })}`)
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}

/**
 * @func 素材编辑回显信息
 * @param {列表对象: record}
 */
const materialGet = `/material/get`
export async function getMaterialGet({ id }) {
  try {
    const response = await http.get(`${requestOnline}${materialGet}?${qs.stringify({ id })}`)
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}

/**
 * @func 素材编辑下架信息
 * @param {列表对象: record}
 */
const materialUnShow = `/material/unShow`
export async function postMaterialUnShow(record) {
  try {
    const response = await http.post(`${requestOnline}${materialUnShow}`, qs.parse(record))
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}
